package dk.fuddi.conversion;

import dk.fuddi.exception.EnDecodeException;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * User: jen
 * Date: May 18, 2007
 * Time: 8:14:34 PM
 */
public class UrlUtils implements DeEncodeInterface {

	private String name = "URL";

	public String encode(String url) throws EnDecodeException {
		System.out.println("UrlUtils.encode");
		try {
			return URLEncoder.encode(url, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			System.err.println("Could not encode URL [" + url + "] " + e.getMessage());
			throw new EnDecodeException(e.getMessage());
		}
	}

	public String decode(String url) throws EnDecodeException {
		System.out.println("UrlUtils.decode");
		try {
			return URLDecoder.decode(url, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			System.err.println("Could not decode url [" + url + "] " + e.getMessage());
			throw new EnDecodeException(e.getMessage());
		}
	}

	public String toString() {
		return name;
	}
}
